﻿using System;
using System.Linq;
using Illinois.Infrastructure.Data;
using Illinois.Infrastructure.Models;
using NUnit.Framework;

namespace Illinois.Infrastructure.Test.Data.Test_IllinoisContext
{
  [TestFixture]
  public class Test_Update_BurnDownData_Items
  {
    private int _entityId;
    private const string Title = "before";
    private const int InitialStoryPoints = 30;

    [SetUp]
    public void Before()
    {
      const int projectId = 815;
      var date = new DateTime(2012, 01, 01);
      var entity = new BurnDownData { Title = Title, InitialStoryPoints = InitialStoryPoints, ProjectId = projectId, Start = date, End = date };

      using (var context = new IllinoisContext())
      {
        context.BurnDowns.Add(entity);
        context.SaveChanges();
        _entityId = entity.Id;
      }
    }

    //[Test]
    //public void Test_I_Update_The_Title()
    //{
    //  const string after = "after";

    //  using (var context = new IllinoisContext())
    //  {
    //    context.UpdateMetaData(_entityId, after, InitialStoryPoints);
    //  }

    //  string title;
    //  using (var context = new IllinoisContext())
    //  {
    //    var entity = context.BurnDowns.Find(_entityId);
    //    title = entity.Title;
    //  }

    //  Assert.AreEqual(after, title);
    //}

    [Test]
    public void Test_I_Update_The_InitialStoryPoints()
    {
      // add some points
      using (var context = new IllinoisContext())
      {
        var entity = context.BurnDowns.Find(_entityId);
        entity.StoryPointsWrapList.Add(new StoryPointsData { BurnDownData = entity, Order = 1, Value = InitialStoryPoints });
        entity.StoryPointsWrapList.Add(new StoryPointsData { BurnDownData = entity, Order = 2, Value = InitialStoryPoints - 5 });
        entity.StoryPointsWrapList.Add(new StoryPointsData { BurnDownData = entity, Order = 3, Value = InitialStoryPoints - 15 });
        context.SaveChanges();
      }

      const int after = 35;

      using (var context = new IllinoisContext())
      {
        context.UpdateMetaData(_entityId, Title, after);
      }

      int points;
      using (var context = new IllinoisContext())
      {
        var entity = context.BurnDowns.Find(_entityId);
        points = entity.InitialStoryPoints;
      }

      Assert.AreEqual(after, points);
    }

    [TearDown]
    public void After()
    {
      using (var context = new IllinoisContext())
      {
        var entity = context.BurnDowns.Find(_entityId);
        foreach (var point in context.StoryPoints.Where(e => e.BurnDownData.Id == _entityId))
        {
          context.StoryPoints.Remove(point);
        }
        context.BurnDowns.Remove(entity);
        context.SaveChanges();
      }
    }
  }
}
